//
//  GolfGame.m
//  Golf
//
//  Created by mrbradle on 11/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GolfGame.h"


@implementation GolfGame

@synthesize deck,deckPos,numRounds,numPlayers,round,turn,players,topCard,drawnCard,knocker,deckEmpty,roundOver,gameOver;

+ (int)MAX_CARDS { return 4; }

- (id)initWithRounds:(int)rounds players:(int)players_ names:(NSMutableArray*)names {
    
    self.gameOver = NO;
    
    self.numRounds = rounds;
    self.numPlayers = players_;
    self.round = 0;
    self.turn = 0;
    
    self.deckEmpty = NO;
    
    self.players = [NSMutableArray arrayWithCapacity:players_];
    for (int i = 0; i < players_; i++) {
        [self.players addObject:[[GolfPlayer alloc] initWithName:[names objectAtIndex:i] cards:[GolfGame MAX_CARDS]]];
    }
    
    self.deck = (int*)malloc(52*sizeof(int));
    for (int i = 0; i < 52; i++) {
        self.deck[i] = i;
    }
    
    return self;
    
}

- (void)shuffle {
    self.deckPos = 0;
    self.deckEmpty = NO;
    
    //SEE THAT.  THERE IS NO FIXING GOING ON BESIDES THAT INHERENT IN THE RANDOM NUMBER ALGORITHM AND THE STRUCTURE OF THE GAME.  THE DEAL IS 100% FAIR YEAH
    for (int i = 0; i < 52; i++) {
        int pos = random()%(52-i)+i;
        int temp = deck[i];
        deck[i] = deck[pos];
        deck[pos] = temp;
    }
}

- (int)dealCard {
    self.deckPos++;
    //NSLog(@"%d",deckPos);
    //NSLog(@"%d",deck[deckPos]);
    if (self.deckPos == 52) {
        self.deckEmpty = YES;
    }
    return deck[deckPos-1];
}

- (void)startRound {
    
    self.roundOver = NO;
    
    self.round++;
    self.turn = (self.round - 1) % self.numPlayers;
    self.drawnCard = -1;
    self.knocker = -1;
    
    [self shuffle];
    
    //Deal cards to each player
    
    for (int l = 0; l < self.numPlayers; l++) {
        GolfPlayer* temp = [self.players objectAtIndex:l];
        for (int i = 0; i < [GolfGame MAX_CARDS]; i++) {
            temp.cards[i] = [self dealCard];
        }
        temp.faceDown[0] = temp.faceDown[1] = YES; //hardcoding 4 again..
        temp.faceDown[2] = temp.faceDown[3] = NO; //hardcoding 4 again.  if I do this app for real this should be changed
    }
    
    self.topCard = [self dealCard]; //Draw one card for the top card
    
    //Wait for player's choice!
}

- (void)swap:(int)target {
    GolfPlayer* temp = [self.players objectAtIndex:turn];
    int temp2 = temp.cards[target];
    temp.faceDown[target] = YES;
    if (self.drawnCard == -1)
        temp.cards[target] = self.topCard;
    else
        temp.cards[target] = self.drawnCard;
    self.topCard = temp2;
    
    [self endTurn];
}

- (void)draw {
    self.drawnCard = [self dealCard];
}

- (void)discard {
    self.topCard = self.drawnCard;
    
    [self endTurn];
}

- (NSString*)getStringFromCard:(int)card {
    NSMutableArray* suits = [NSMutableArray arrayWithObjects:@"♣", @"♦", @"♥", @"♠", nil];
    NSMutableArray* ranks = [NSMutableArray arrayWithObjects:@"A", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"J", @"Q", @"K", nil];
    
    return [NSString stringWithFormat:@"%@%@", [ranks objectAtIndex:(card) % 13], [suits objectAtIndex:(int)(card/13)]];
}

- (void)knock {
    self.knocker = self.turn;
    
    [self endTurn];
}

- (void)endTurn {
    self.drawnCard = -1; //End the turn
    self.turn = (self.turn + 1) % self.numPlayers;
    
    if (self.turn == self.knocker) self.roundOver = YES; //If someone knocked, end the round on his next turn
}

- (int)getCardValue:(int)card {
    int temp = card % 13 + 1; //suit is irrelevant, just get value
    if (temp == 11 || temp == 12) temp = 10; //jack or queen = 10
    if (temp == 13) temp = 0; //king = 0
    return temp;
}

- (int)getHandValue:(int)player {
    int temp = 0;
    for (int i = 0; i < [GolfGame MAX_CARDS]; i++) {
        temp += [self getCardValue:((GolfPlayer*)[self.players objectAtIndex:player]).cards[i]];
    }
    return temp;
}

- (void)endRound {
    //self.roundOver = YES; //set this.  it's required to make the view work.  yay adding a useless variable that turned out useful
    
    //Add the scores for each player
    for (int i = 0; i < self.numPlayers; i++) {
        GolfPlayer* player = (GolfPlayer*)[self.players objectAtIndex:i];
        player.roundScore = [self getHandValue:i];
        player.score += player.roundScore;
        /*NSLog([NSString stringWithFormat:@"Player %d cards %d %d %d %d",i,((GolfPlayer*)[self.players objectAtIndex:i]).cards[0]
              ,((GolfPlayer*)[self.players objectAtIndex:i]).cards[1]
              ,((GolfPlayer*)[self.players objectAtIndex:i]).cards[2]
              ,((GolfPlayer*)[self.players objectAtIndex:i]).cards[3]]);*/
    }
    
    //Nothing else to be done here for now cause all the round stuff is handled in startRound
}

- (void)endGame {
    self.gameOver = YES;
    
    //sort list of players.  this is highly specialized forend game of course
    
    for (int i = 0; i < self.numPlayers; i++) { //insertion sort cause I'm too lazy to do better
        GolfPlayer* temp = [players objectAtIndex:i];
        int index = i;
        for (int j = i; j < self.numPlayers; j++) {
            GolfPlayer* temp2 = [players objectAtIndex:j];
            if (temp2.score < temp.score) {
                temp = temp2;
                index = j;
            }
        }
        [players exchangeObjectAtIndex:i withObjectAtIndex:index];
    }
    
}

@end
